package com.eddie.community.controller;

import com.eddie.community.entity.Comment;
import com.eddie.community.entity.Event;
import com.eddie.community.entity.Page;
import com.eddie.community.entity.User;
import com.eddie.community.event.EventProducer;
import com.eddie.community.service.StatisticsService;
import com.eddie.community.service.UserService;
import com.eddie.community.util.CommunityConstant;
import com.eddie.community.util.CommunityUtil;
import com.eddie.community.util.HostHolder;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:数据统计视图层
 * Date: 2021/3/4 0004 18:17
 * Author: Eddie.Zhan
 * Email: eddie_zhan@yeah.net
 */
@Controller
public class StatisticsController implements CommunityConstant {
    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private UserService userService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private EventProducer eventProducer;

    // 统计页面
    @RequestMapping(path = "/data", method = {RequestMethod.GET, RequestMethod.POST})
    public String getDataPage(Model model, Page page) {

        List<User> userVoList = userService.findHasRoleUser(page.getOffset(), page.getLimit());

        page.setLimit(6);
        page.setPath("/data");
        page.setRows(userVoList.size());

        model.addAttribute("users", userVoList);
        return "/site/admin/data";
    }


    // 权限管理
    @RequestMapping(path = "/data/authority", method = RequestMethod.POST)
    @ResponseBody
    public String authority(int userId, int type) {
        userService.changeType(userId, type);
        // 触发变更类型事件
        Event event = new Event()
                .setTopic(TOPIC_CHANGE)
                .setEntityId(type)
                .setEntityUserId(userId);
        eventProducer.fireEvent(event);
        return CommunityUtil.getJSONString(0);
    }

    // 统计网站UV
    @RequestMapping(path = "/data/uv", method = RequestMethod.POST)
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
        long uv = statisticsService.calculateUV(start, end);
        model.addAttribute("uvResult", uv);
        model.addAttribute("uvStartDate", start);
        model.addAttribute("uvEndDate", end);
        return "forward:/data";
    }

    // 统计活跃用户
    @RequestMapping(path = "/data/dau", method = RequestMethod.POST)
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model) {
        long dau = statisticsService.calculateDAU(start, end);
        model.addAttribute("dauResult", dau);
        model.addAttribute("dauStartDate", start);
        model.addAttribute("dauEndDate", end);
        return "forward:/data";
    }


}
